﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;
using Entities;
using DevExpress.XtraReports.UI;

namespace QLTTTH
{
    public partial class UCHocPhi : DevExpress.XtraEditors.XtraUserControl
    {
        public UCHocPhi()
        {
            InitializeComponent();
        }

        private void UCHocPhi_Load(object sender, EventArgs e)
        {
            LoadHocPhi();
            grvDSHocPhi.OptionsSelection.MultiSelect = true;
        }


        private void LoadHocPhi()
        {
            var aResult = HocPhiManager.LayDanhSachBienLai();
            if (aResult.Success)
                gridDanhSach.DataSource = aResult.Result;
            //grvDSChungChi.DataSource = aResult.Result;
        }

        private void btnTimKiem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            grvDSHocPhi.ShowFindPanel();
        }



        private void btnThem_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmThongTinHocPhi frm = new frmThongTinHocPhi();
            frm.sendData = new frmThongTinHocPhi.SendData(ThemHocPhi);
            frm.ShowDialog();
        }

        private void ThemHocPhi(BienLai hocphi)
        {
            var aResult = HocPhiManager.ThemHocPhi(hocphi);
            if (aResult.Success)
            {
                LoadHocPhi();
                var lst = new List<BienLaiIn>();
                var bl = new BienLaiIn();
                bl.MaBienLai = aResult.Result;
                var hpResult = HocPhiManager.TimhHocPhi(aResult.Result, (object a, object b) =>
                            {
                                return (b as BienLai).MaBienLai.CompareTo(a);
                            });
                if (hpResult.Success)
                {
                    bl.MaDangKy = hpResult.Result.MaDangKy;
                    bl.MaHV = hpResult.Result.MaHV;
                    bl.MaNV = hpResult.Result.MaNV;
                    bl.TienHP = hpResult.Result.TienHP;
                    bl.TenHV = HocVienManager.TimDanhSachHocVien(hpResult.Result.MaHV, (object a, object b) =>
                            {
                                return (b as HocVien).MaHV.CompareTo(a);
                            }).Result[0].HoTen;
                    bl.TenNV = NhanVienManager.TimDanhSachNhanVien(hpResult.Result.MaNV, (object a, object b) =>
                            {
                                return (b as NguoiDung).MaNV.CompareTo(a);
                            }).Result[0].HoTen;
                    lst.Add(bl);
                }

                var rpt = new rptBienLaiIn();
                rpt.DataSource = lst;
                var tool = new ReportPrintTool(rpt);
                tool.ShowPreview();

            }
        }

        private void btnXemChiTiet_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmThongTinHocPhi frm = new frmThongTinHocPhi();
            frm.FillControl(LayBienLai_GrvDanhSach());
            frm.btnLuu.Enabled = false;
            frm.ShowDialog();
        }
        private BienLai LayBienLai_GrvDanhSach()
        {
            var hp = new BienLai();
            hp.MaBienLai = int.Parse(grvDSHocPhi.GetFocusedRowCellValue("MaBienLai").ToString());
            hp.MaDangKy = int.Parse(grvDSHocPhi.GetFocusedRowCellValue("MaDangKy").ToString());
            hp.MaHV = int.Parse(grvDSHocPhi.GetFocusedRowCellValue("MaHV").ToString());
            hp.MaNV = int.Parse(grvDSHocPhi.GetFocusedRowCellValue("MaNV").ToString());
            hp.TienHP = decimal.Parse(grvDSHocPhi.GetFocusedRowCellValue("TienHP").ToString());
            hp.NgayThu = DateTime.Parse(grvDSHocPhi.GetFocusedRowCellValue("NgayThu").ToString());
            return hp;
        }



        private void btnXoa_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (XtraMessageBox.Show("Bạn chắc chắn xóa học phí này", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                int maBienLai;

                for (int i = 0; i < grvDSHocPhi.RowCount; i++)
                {
                    if (grvDSHocPhi.IsRowSelected(i))
                    {
                        maBienLai = int.Parse(grvDSHocPhi.GetRowCellDisplayText(i, "MaBienLai").ToString());
                        HocPhiManager.XoaHocPhi(maBienLai);
                    }
                }

                grvDSHocPhi.DeleteSelectedRows();

            }
        }



        private void CapNhatHocPhi(BienLai hocphi)
        {
            HocPhiManager.CapNhatHocPhi(hocphi);
            grvDSHocPhi.SetFocusedRowCellValue("MaDangKy", hocphi.MaDangKy);
            grvDSHocPhi.SetFocusedRowCellValue("MaHV", hocphi.MaHV); 
            grvDSHocPhi.SetFocusedRowCellValue("MaNV", hocphi.MaNV);
            grvDSHocPhi.SetFocusedRowCellValue("NgayThu", hocphi.NgayThu);
            grvDSHocPhi.SetFocusedRowCellValue("TienHP", hocphi.TienHP);
        }
        private void btnCapNhap_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            frmThongTinHocPhi frm = new frmThongTinHocPhi();
            frm.FillControl(LayBienLai_GrvDanhSach());
            frm.sendData = new frmThongTinHocPhi.SendData(CapNhatHocPhi);
            frm.ShowDialog();
        }

    

    }
}
